/*
 * GotoIfFalse.cpp
 *
 *  Created on: 17.04.2012
 *      Author: Eugene Kornykhin
 */

#include "GotoIfFalse.h"
#include "Link.h"
#include "BooleanConst.h"
#include <stdexcept>
#include <typeinfo>

GotoIfFalse::GotoIfFalse() : destination(0) {

}

GotoIfFalse::~GotoIfFalse() {
}

void GotoIfFalse::execute(Interpreter& interpreter) {
	const PElement &d = interpreter.popPolizElement();
	const PElement &c = interpreter.popPolizElement();
	try {
		const Link &dest = dynamic_cast<const Link &>(d);
		const BooleanConst &cond = dynamic_cast<const BooleanConst &>(c);
		condition = cond.isTrue();
		destination = dest.getDestination();
	} catch(std::bad_cast&) {
		throw std::logic_error("GotoIfFalse excepts a link and a boolean constant");
	}
}

PolizPosition GotoIfFalse::next_position_after(PolizPosition p) const {
	if (condition) {
		return p.next();
	} else {
		return destination;
	}
}

bool GotoIfFalse::isHalt() const {
	return false;
}

GotoIfFalse* GotoIfFalse::clone(const Poliz &) const {
	return new GotoIfFalse();
}

string GotoIfFalse::toString() const {
	return "[!F]";
}
